//
//  BLRepertoryViewController.m
//  blanket
//
//  Created by Mac on 2017/12/6.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "BLRepertoryViewController.h"
#import "BLSupportTableViewCell.h"
#import "BLRepertoryModel.h"
#import "BLRecoedModel.h"
@interface BLRepertoryViewController ()
@property(nonatomic, strong) BLRepertoryModel                     *model;
@property(nonatomic, strong) NSMutableArray                     *labelArray;
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation BLRepertoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查看库存";
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self configSubView];
    [self sendMyRepertoryRequest];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    self.labelArray = [NSMutableArray new];
    STLabel * lable = [[STLabel alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60, 40)
                                                text:@"当前库存:"
                                           textColor:BL_firstTextColor
                                                font:15
                                         isSizetoFit:NO
                                       textAlignment:NSTextAlignmentLeft];
    [header addSubview:lable];
    
    CGFloat labelWith = UIScreenWidth/2 - 45;
    CGFloat bootom;
    NSArray * array = @[@"床单1.8",@"被套1.8",@"床单1.5",@"被套1.5",@"床单1.2",@"被套1.2",@"枕套",@"面巾",@"浴巾"];
    for (int i = 0; i < 9; i ++) {
        NSString * title = array[i];
        STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(30 * (i%2) + 30 + labelWith * (i%2), lable.bottom + 50 * (i/2), labelWith, 40)
                                                         text:[NSString stringWithFormat:@"  %@:0",title]
                                                    textColor:BL_firstTextColor
                                                         font:14
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
        titleLable.backgroundColor = BL_backgroundColor;
        [header addSubview:titleLable];
        
        bootom = titleLable.bottom;
        [self.labelArray addObject:titleLable];
    }
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(30, bootom + 20, UIScreenWidth - 60, 40)
                                                     text:@"库存详情"
                                                textColor:BL_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    //sectionheader
    STLabel * pingzhong = [self defultlableWithTitle:@"品种"];
    [pingzhong st_showLeftLine:pingzhong.height];
    pingzhong.top = titleLable.bottom  + 10;
    pingzhong.left = 30;
    [header addSubview:pingzhong];
    STLabel * shijian = [self defultlableWithTitle:@"时间"];
    shijian.top = titleLable.bottom  + 10;
    shijian.left = pingzhong.right;
    [header addSubview:shijian];
    STLabel * chuku = [self defultlableWithTitle:@"出库"];
    chuku.top = titleLable.bottom  + 10;
    chuku.left = shijian.right;
    [header addSubview:chuku];
    STLabel * ruku = [self defultlableWithTitle:@"入库"];
    [ruku st_showRightLine:ruku.height];
    ruku.top = titleLable.bottom  + 10;
    ruku.left = chuku.right;
    [header addSubview:ruku];
    header.height = ruku.bottom;
    self.tableView.tableHeaderView = header;
    
}
- (STLabel*)defultlableWithTitle:(NSString*)title{
    CGFloat sectionLableWith = (UIScreenWidth - 60)/4;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, sectionLableWith, 30)
                                                     text:title
                                                textColor:BL_secendTextColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [titleLable st_showTopLine];
    [titleLable st_showBottomLine];
    return titleLable;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BLSupportTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BLSupportTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BLSupportTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Private Method
- (void)reloadData{
    
    if (self.labelArray.count != self.model.stkeyArray.count) {
        //[SVProgressHUD showErrorWithStatus:@"不能增加字段"];
        return;
    }
    NSArray * stkeyArray = self.model.stkeyArray;
    NSMutableArray * finshArray = [NSMutableArray arrayWithCapacity:9];
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"床1.8"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"被子1.8"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"床1.5"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"被子1.5"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"床1.2"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"被子1.2"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"枕套"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"面巾"]) {
            [finshArray addObject:dic];
        }
    }
    for (NSDictionary * dic in stkeyArray) {
        NSString * name = dic[@"BLRepertoryModelTitleKey"];
        if ([name containsString:@"浴巾"]) {
            [finshArray addObject:dic];
        }
    }
    for (int i = 0; i < self.labelArray.count; i ++ ) {
        UILabel * label = self.labelArray[i];
        NSDictionary * dic = finshArray[i];
        NSString * title = dic[BLRepertoryModelTitleKey];
        NSString * num = dic[BLRepertoryModelNumKey];
        label.text = [NSString stringWithFormat:@" %@:%@",title,num];
    }
}
- (void)configDataSouceWithRepordArray:(NSArray<BLRepertoryModel*>*)array{
    self.dataSouce = [NSMutableArray new];
    for (BLRepertoryModel * reprot in array) {
        for (int i = 0; i < self.labelArray.count; i ++ ) {
            NSDictionary * dic = reprot.stkeyArray[i];
            NSString * title = dic[BLRepertoryModelTitleKey];
            NSString * num = dic[BLRepertoryModelNumKey];
            BLRecoedModel * model = [BLRecoedModel new];
            if ([reprot.type isEqualToString:@"2"]) {
                model.chuku = num;
                model.ruku = 0;
            }else{
                model.ruku = num;
                model.chuku = 0;
            }
            model.name = title;
            NSDate * date = [NSDate dateWithTimeIntervalSince1970:[reprot.create_time integerValue]];
            model.time = date.st_yyyyMMddhhmmssWithDate;
            [self.dataSouce addObject:model];
        }

    }
    [self.tableView reloadData];
}
#pragma mark --NetWork Method
- (void)sendMyRepertoryRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:debugToken forKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php/Api/Basics/currentStockAction"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * resDic = responseObject;
                                                 NSLog(@"json\n%@",resDic.mj_JSONString);
                                                 NSDictionary * dataDic =  responseObject[@"data"][@"current"];
                                                 if (![dataDic isKindOfClass:[NSNull class]]) {
                                                     self.model = [BLRepertoryModel mj_objectWithKeyValues:dataDic];
                                                     self.model.originDic = dataDic;
                                                 }

                                                 NSMutableArray * reports = [NSMutableArray new];
                                                 id logobj =  responseObject[@"data"][@"log"];
                                                 if ([logobj isKindOfClass:NSArray.class]) {
                                                     for (NSDictionary * modelDic in responseObject[@"data"][@"log"]) {
                                                         BLRepertoryModel * model = [BLRepertoryModel mj_objectWithKeyValues:modelDic];
                                                         model.originDic = modelDic;
                                                         [reports addObject:model];
                                                     }
                                                 }

                                                 [self configDataSouceWithRepordArray:reports];
                                                 [self reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end
